﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using System.Data;
using System.Data.SqlClient;
using Support.DTO;

namespace DataAccess
{
    public class HabitoToxicoDAL : BaseSQLDAL
    {
        public void GuardarHabitoToxico(HabitoToxicoDTO habitoToxico)
        {
            try
            {
                connection = new SqlConnection(Support.Configuration.ConfigurationManager.ConnectionString_Sindicato);
                CreateCommand("HabitoToxico_Guardar", true);
                AddCmdParameter("@TipoHabitoId", habitoToxico.TipoHabitoId, ParameterDirection.Input);
                AddCmdParameter("@HistoriaId", habitoToxico.HistoriaId, ParameterDirection.Input);

                ExecuteReader();
                Read();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeCommand();
                DisposeDataReader();
                CloseConnection();
            }
        }


        private HabitoToxicoDTO FillDTO(SqlDataReader reader)
        {
            return new HabitoToxicoDTO()
            {
                TipoHabitoId = Convert.ToInt32(GetOutResult("tipoHabitoId")),
                HistoriaId = Convert.ToInt32(GetOutResult("historiaId")),
                NombreHabito = Convert.ToString(GetOutResult("nombre")),
                Tiene = true
            };
        }

        public List<HabitoToxicoDTO> ObtenerPorHistoriaId(int historiaId)
        {
            var lista = new List<HabitoToxicoDTO>();

            try
            {

                CreateCommand("HabitoToxico_ObtenerPorHistoriaId", true);
                AddCmdParameter("@HistoriaId", historiaId, ParameterDirection.Input);

                ExecuteReader();
                while (Read())
                {
                    lista.Add(FillDTO(GetDataReader()));
                }
                return lista;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeCommand();
                DisposeDataReader();
                CloseConnection();
            }
        }

        public void EliminarPorHistoriaId(int historiaId)
        {
            try
            {

                CreateCommand("HabitoToxico_EliminarPorHistoriaId", true);
                AddCmdParameter("@HistoriaId", historiaId, ParameterDirection.Input);

                ExecuteReader();
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                DisposeCommand();
                DisposeDataReader();
                CloseConnection();
            }
        }
    }
}
